package com.azxc.rapid.plus.system.feign;

import com.azxc.rapid.core.mp.support.Condition;
import com.azxc.rapid.core.tenant.annotation.NonDS;
import com.azxc.rapid.core.tool.api.R;
import com.azxc.rapid.core.tool.utils.Func;
import com.azxc.rapid.plus.system.service.IDeptPlusService;
import com.azxc.rapid.plus.system.service.IRegionPlusService;
import com.azxc.rapid.system.entity.Dept;
import com.azxc.rapid.system.entity.Region;
import com.azxc.rapid.system.vo.RegionTreeVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author linym
 * @className: RegionPlusClient
 * @description:
 * @date 2022/9/2 16:30
 * @version：1.0
 */

@NonDS
@ApiIgnore
@RestController
@AllArgsConstructor
public class RegionPlusClient implements IRegionPlusClient{

	private final IRegionPlusService service;
	private final IDeptPlusService deptService;

	@Override
	@GetMapping(GET_CITY_LIST)
	public R<List<Region>> getCityList(String cityCode) {
		List<Region> regionList = service.list(Wrappers.<Region>query().lambda()
			.eq(Region::getParentCode, cityCode)
			.orderByAsc(Region::getSort)
		);
		return R.data(regionList);
	}

	@Override
	@GetMapping(GET_JURISDICTION_LIST_CITY)
	public R<List<Region>> getJurisdictionByCity(String cityCode,String jurisdictionLevel) {
		if (Func.isEmpty(cityCode)) {
			return R.data(null);
		}
		return R.data(service.list(Wrappers.<Region>query().lambda()
			.eq(Region::getRegionLevel, jurisdictionLevel)
			.likeRight(Region::getCode, cityCode).orderByAsc(Region::getSort)));
	}

	@Override
	@GetMapping(GET_BY_CODE)
	public R<Region> getByCode(String code) {
		return R.data(service.getOne(Wrappers.<Region>query().lambda().eq(Region::getCode, code)));
	}

	@Override
	@GetMapping(GET_BY_CODES)
	public R<List<Region>> getByCodes(String codes) {
		List<Long> codeList = Func.toLongList(codes);
		return R.data(service.list(
			Wrappers.<Region>query().lambda().in(Region::getCode,codeList).orderByAsc(Region::getSort)));
	}

	@Override
	@GetMapping(GET_BY_PARAMS)
	public R<List<Region>> getByParams(Region region) {
		return R.data(service.list(Condition.getQueryWrapper(region)));
	}

	@Override
	@GetMapping(GET_BY_DEPT)
	public R<Region> getByDept(Long deptId) {
		Dept dept = deptService.getById(deptId);
		return R.data(dept == null ? null : service.getById(dept.getRegionCode()));
	}

	@Override
	@GetMapping(GET_BY_GENERATE_DOC)
	public R<Region> getByGenerateDoc(String code) {
		Region region = service.getOne(Wrappers.<Region>query().lambda().eq(Region::getCode, code));
		if(region == null) {
			return R.data(null);
		}
//		while(region.getIsGenerateDoc() != 1) {
//			region = service.getOne(Wrappers.<Region>query().lambda().eq(Region::getCode, region.getParentCode()));
//		}
		return R.data(region);
	}

	@Override
	public R<List<Region>> getChildRegionsByCode(String code) {
		List<Region> regionList = service.list(Wrappers.<Region>query().eq("parent_code", code));
		return R.data(regionList);
	}

	@Override
	public R<Region> getRegionByCode(String code) {
		Region region = service.getOne(Wrappers.<Region>query().eq("code",code));
		return R.data(region);
	}

	@Override
	public R<List<Region>> getList() {
		List<Region> regionList = service.list();
		return R.data(regionList);
	}

	@Override
	public R<String> selectRegionCode(String city) {
		//select a.code from blade_region a where a.name like CONCAT('%',#{city},'%')  and  LENGTH(a.code)>4
		List<Region> regionList = service.list(Wrappers.<Region>lambdaQuery().like(Region::getName,city).lt(Region::getCode,4));
		return R.data(regionList.stream().findFirst().get().getCode());
	}

	public R<RegionTreeVO> getTreeByCode(String parentCode) {
		RegionTreeVO treeVO = service.getTreeByCode(parentCode);
		return R.data(treeVO);
	}

	/**
	 * 根据行政
	 *
	 * @param regionName
	 * @return
	 */
	@Override
	public R<Region> getRegionByName(String regionName) {
		Region region = new Region();
		region.setName(regionName);
		Region data = service.getOne(Condition.getQueryWrapper(region));
		return R.data(data);
	}

}
